import Mock from "mockjs"; //导入mockjs
import {decrypt} from "@/utils/jsencrypt";
import {MUSIC_LIST} from "@/mock/table/MUSIC_LIST";
import {RESOURCE_LIST} from "@/mock/table/RESOURCE_LIST";
import {FLEX_LIST} from "@/mock/table/FLEX_LIST";
import {SLIDES_LIST} from "@/mock/table/SLIDES_LIST";

// 阻止mockjs改变原始的XMLHttpRequest文件获取
// @ts-ignore
Mock.XHR.prototype.send = (() => {
    // @ts-ignore
    const _send = Mock.XHR.prototype.send;
    // @ts-ignore
    return function (this: Mock.XHR) {
        if (!this.match) {
            this.custom["xhr"].responseType = this.responseType || "";
            this.custom["xhr"].timeout = this.timeout || 0;
            this.custom["xhr"].withCredentials = this.withCredentials || false;
            this.custom["xhr"].onabort = this.onabort || null;
            this.custom["xhr"].onerror = this.onerror || null;
            this.custom["xhr"].onload = this.onload || null;
            this.custom["xhr"].onloadend = this.onloadend || null;
            this.custom["xhr"].onloadstart = this.onloadstart || null;
            this.custom["xhr"].onprogress = this.onprogress || null;
            this.custom["xhr"].onreadystatechange = this.onreadystatechange || null;
            this.custom["xhr"].ontimeout = this.ontimeout || null;
        }
        return _send.apply(this, arguments);
    };
})();
//使用Mock下面提供的mock方法进行需要模拟数据的封装
//参数1，是需要拦截的完整请求地址，参数2，是请求方式，参数3，是请求的模拟数据
// 启用 Mock.js
Mock.setup({
    timeout: 500, // 模拟数据延迟返回的时间，单位毫秒
});

// 获取瀑布流资源数据
Mock.mock("/resourceList", "post", (options) => {
    const {page, size, type} = JSON.parse(options.body);
    const startIndex = (page - 1) * size;
    const endIndex = startIndex + size;
    // 根据不同的type条件筛选资源列表
    let filteredData;
    if (type === 0) {
        filteredData = RESOURCE_LIST.slice(startIndex, endIndex);
    } else {
        filteredData = RESOURCE_LIST.filter(resource => resource.resourceType === type)
            .slice(startIndex, endIndex);
    }
    return {
        code: 200,
        data: filteredData,
    };
});


// 获取Flex数据
Mock.mock("/flexList", "get", () => {
    return {
        code: 200,
        data: FLEX_LIST,
    };
});


// 获取Flex数据
Mock.mock("/slides/list", "get", () => {
    return {
        code: 200,
        data: SLIDES_LIST,
    };
});


// 获取音乐数据
Mock.mock("/getMusic", "get", (options) => {
    return {
        code: 200,
        data: MUSIC_LIST,
    };
});

// 登录
Mock.mock("/login", "post", (option) => {
    const {password} = JSON.parse(option.body);
    const pwd = decrypt(password);
    if (Number(pwd) === 123456) {
        return {
            code: 200,
            msg: "操作成功",
            data: password,
        };
    } else {
        return {
            code: 401,
            msg: "密码错误",
        };
    }
});

// Token验证方法
Mock.mock("/verification", "post", (option) => {
    const {token} = JSON.parse(option.body);
    const pwd = decrypt(token);
    if (Number(pwd) === 123456) {
        return {
            code: 200,
            msg: "操作成功",
        };
    } else {
        return {
            code: 401,
            msg: "登录已过期，请重新登录",
        };
    }
});
